package ltd.simulatoruidemo.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;

import java.util.List;

import ltd.simulatoruidemo.R;
import ltd.simulatoruidemo.activities.MainActivity;
import ltd.simulatoruidemo.entity.Layout;
import ltd.simulatoruidemo.util.CollectionUtil;

/**
 * Created by chinalin on 2017/5/9.
 */

public class LayoutFragment extends Fragment {
    private List<Layout> fragmentList ;
    public static final String ARGUMENT = "LayoutList";
    private final String internetUrl = "http://192.168.2.156:8080/serverapi/resources/游戏推荐691/";
//    private String mArgument;
    RelativeLayout relativeLayoutFragment ;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_layout, container,false);
//        HorizontalScrollView hScrollview = (HorizontalScrollView)view.findViewById(R.id.HScrollview);
        relativeLayoutFragment = (RelativeLayout)view.findViewById(R.id.relativeLayoutFragment);

        if (CollectionUtil.isNotEmpty(fragmentList)){
            Layout layout ;
            for ( int i=0 ; i< fragmentList.size();i++){
                layout = fragmentList.get(i);
                ImageButton button = new ImageButton(getContext());
//                button.setText(layout.getName());
                MainActivity.SetReflectMargins(layout.getX(),layout.getY(),layout.getW(),layout.getH(),button);
                String picUrl = internetUrl + "icon_" + layout.getMode() + "_" + layout.getDevice() + ".png";
                if (i == 0){
                    Log.e("LayoutFragment:picUrl" , picUrl) ;
                }
                Glide
                        .with(getContext())
                        .load(picUrl)
                        .into(button);
//                RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(layout.getW(),layout.getH());
//                lp.setMargins(layout.getX(),layout.getY(), 0, 0);
//                button.setLayoutParams(lp);
                button.setScaleType(ImageView.ScaleType.FIT_XY);
                button.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (hasFocus){
                            v.setScaleX(1.1f);
                            v.setScaleY(1.1f);
                        }else{
                            v.setScaleX(1.0f);
                            v.setScaleY(1.0f);
                        }
                    }
                });
//                hScrollview.addView(button);
                relativeLayoutFragment.addView(button);
            }
        }
        return view;
    }
    /**
     * 传入需要的参数，设置给arguments
     * @param argument
     * @return
     */
    public static LayoutFragment newInstance(String argument)
    {
        Bundle bundle = new Bundle();
        bundle.putString(ARGUMENT, argument);
        LayoutFragment contentFragment = new LayoutFragment();
        contentFragment.setArguments(bundle);
        return contentFragment;
    }
    /**
     * 传入需要的参数，设置给fragmentList
     * @param fragmentList 布局list
     * @return
     */
    public static LayoutFragment newInstance(List<Layout> fragmentList)
    {
//        Bundle bundle = new Bundle();
//        bundle.putString(ARGUMENT, argument);
        LayoutFragment layoutFragment = new LayoutFragment();
        layoutFragment.fragmentList = fragmentList ;
//        contentFragment.setArguments(bundle);
        return layoutFragment;
    }
}
